<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ns1="ddi:instance:3_1" xmlns:a="ddi:archive:3_1" xmlns:r="ddi:reusable:3_1" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:dc="ddi:dcelements:3_1" xmlns:ns7="http://purl.org/dc/elements/1.1/" xmlns:cm="ddi:comparative:3_1" xmlns:d="ddi:datacollection:3_1" xmlns:l="ddi:logicalproduct:3_1" xmlns:c="ddi:conceptualcomponent:3_1" xmlns:ds="ddi:dataset:3_1" xmlns:p="ddi:physicaldataproduct:3_1" xmlns:pr="ddi:ddiprofile:3_1" xmlns:s="ddi:studyunit:3_1" xmlns:g="ddi:group:3_1" xmlns:pi="ddi:physicalinstance:3_1" xmlns:m3="ddi:physicaldataproduct_ncube_inline:3_1" xmlns:m1="ddi:physicaldataproduct_ncube_normal:3_1" xmlns:m2="ddi:physicaldataproduct_ncube_tabular:3_1" xmlns:xf="http://www.w3.org/2002/xforms">
	<xsl:template match="/">
				<xsl:apply-templates select="//*:Code" mode="make_page"/>
	</xsl:template>
	<xsl:template match="*:Code" mode="make_page">
		<xsl:variable name="categoryID">
			<xsl:value-of select="*:CategoryReference/*:ID"/>
		</xsl:variable>
		<xsl:result-document href="./ddi-codebook/{$categoryID}.html">
			<html>
				<head></head>
				<body>
					<xsl:apply-templates select="ancestor::*:Code" mode="breadcrumbs"/>
					<h2>
						<xsl:apply-templates select="*:Value" /> - <xsl:value-of select="//*:Category[@id=$categoryID]/*:Label"/>
					</h2>
					<p>
						<xsl:copy-of select="//*:Category[@id=$categoryID]/*:Description"/>
					</p>
					<xsl:if test="exists(./Code)">
					<p>
						<b>Including the following classifications:</b>
					</p>
					<xsl:apply-templates select="*:Code" mode="make_list"/>
					</xsl:if>
				</body>
			</html>
		</xsl:result-document>
	</xsl:template>
	
	<xsl:template match="*:Code" mode="make_list">
		<xsl:variable name="categoryID">
			<xsl:value-of select="*:CategoryReference/ID"/>
		</xsl:variable>
		<ul>
			<xsl:element name="a">
				<xsl:attribute name="href">./<xsl:value-of select="$categoryID"/>.html</xsl:attribute>
				<xsl:attribute name="target">mainFrame</xsl:attribute>
				<xsl:apply-templates select="*:Value" /> - <xsl:value-of select="//*:Category[@id=$categoryID]/*:Label"/>
			</xsl:element>
		</ul>
	</xsl:template>
	<xsl:template match="*:Label">
		<xsl:copy-of select="."/>
	</xsl:template>

	<xsl:template match="*:Code" mode="breadcrumbs">
                <xsl:variable name="categoryID">
                        <xsl:value-of select="*:CategoryReference/*:ID"/>
                </xsl:variable>
		<span class="breadcrumb">
			<xsl:element name="a">
				<xsl:attribute name="href">./<xsl:value-of select="$categoryID"/>.html</xsl:attribute>
				<xsl:attribute name="target">mainFrame</xsl:attribute>
				<xsl:apply-templates select="*:Value" /> - <xsl:value-of select="//*:Category[@id=$categoryID]/*:Label"/>
			</xsl:element>
		</span> &gt;
	</xsl:template>
</xsl:stylesheet>
